<template>
  <div class="home">
    <nav>
      <h2>主页</h2>
      <div class="address-icon">
        <SearchIcon />
        <AddComponent />
      </div>
    </nav>
    <div class="home-list">
      <div
        v-show="cacheGroupItem !== ''"
        class="create-grop-item"
        @click="routerNewGroupChatPage"
      >
        <img src="../assets/image/images/default_hd_avatar.png" />
        <span>{{ cacheGroupItem }}</span>
      </div>
    </div>
  </div>
</template>

<script>
import AddComponent from "../components/AddComponent.vue";
import SearchIcon from "../components/SearchIcon.vue";
import { mapState } from "vuex";
import WebIM from "../utils/WebIM";
export default {
  name: "HomeView",
  data() {
    return {
      // TODO 放在列表中渲染(单聊,群聊)
      cacheGroupItem: "",
    };
  },
  components: {
    AddComponent,
    SearchIcon,
  },
  computed: {
    // User.js会创建一个变量 createGroupAcceptNews 存储是有新创建群聊的消息,如果有则渲染页面
    ...mapState({
      createGroupAcceptNews: (state) => state.user.createGroupAcceptNews,
    }),
  },
  watch: {
    createGroupAcceptNews() {
      this.getCreatGroupItem();
    },
  },
  created() {
    this.getCreatGroupItem();
  },
  methods: {
    getCreatGroupItem() {
      let option = {
        // 群组 ID 或群组 ID 数组。
        groupId: this.createGroupAcceptNews.newGroupId,
      };
      if (option.groupId !== undefined) {
        WebIM.conn.getGroupInfo(option).then((res) => {
          this.cacheGroupItem = res.data[0].name;
        });
      }
    },
    routerNewGroupChatPage() {
      this.$router.push({
        name: "newGroupChatPage",
        params: {
          groupId: this.createGroupAcceptNews.newGroupId,
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.home {
  nav {
    position: relative;
    height: 45px;
    background-color: rgb(17, 17, 17);
    color: white;
    text-align: center;
    line-height: 45px;

    .address-icon {
      width: 60px;
      position: absolute;
      right: 15px;
      top: 10px;
      display: flex;
      justify-content: space-between;
    }
  }

  .home-list {
    color: rgb(164, 164, 164);
    width: 100%;
    height: 88vh;
    background-color: rgb(20, 20, 20);
    .create-grop-item {
      background-color: rgb(67, 67, 67);
      padding: 8px 15px;
      display: flex;
      flex-direction: row;
      align-items: center;
      img {
        width: 40px;
        height: 40px;
        margin-right: 10px;
      }
    }
  }
}
</style>